package com.returnsoft.resource.service;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.OjtDto;
import com.returnsoft.resource.eao.OjtEao;
import com.returnsoft.resource.entity.Ojt;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class OjtBean
 */
@Stateless
@LocalBean
public class OjtBean implements OjtInterface {

	@EJB
	private OjtEao eao;

	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public OjtBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<OjtDto> findList(List<Integer> areasId,
			List<Integer> subAreasId, Integer recruiterId,String ojtFormattedAt, Integer stateId,
			String candidateDocumentNumber, String candidateFirstname,
			String candidateLastname) throws RecruitmentException {

		try {

			List<OjtDto> entities = eao.findList(areasId, subAreasId,recruiterId,ojtFormattedAt, stateId,
					candidateDocumentNumber, candidateFirstname,
					candidateLastname);

			return entities;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}

	}

	@Override
	public OjtDto add(OjtDto dto) throws RecruitmentException {
		try {

			Ojt entity = conv.toOjt(dto);
			entity = eao.add(entity);
			dto = conv.fromOjt(entity);

			return dto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public OjtDto findById(Integer id) throws RecruitmentException {
		try {
			Ojt entity = eao.findById(id);
			OjtDto dto = conv.fromOjt(entity);
			return dto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}
	
	@Override
	public List<OjtDto> findPendingsByCandidateId(Integer candidateId)
			throws RecruitmentException {
		try {
			List<OjtDto> ojts = eao
					.findPendingsByCandidateId(candidateId);
			return ojts;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
